//---------------Sonic CD Fall Platform Script----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
#alias Object.Value0	:	Object.Falling
#alias Object.Value1	:	Object.Timer
#alias Object.Value2	:	Object.YPlayerFloor
#alias Object.Value3	:	Object.FallTimer
#alias Object.Value4	:	Object.YVelocity
#alias Object.Value5	:	Object.YOriginPos

// States
#alias 0	:	FALLPLATFORM_IDLE
#alias 1	:	FALLPLATFORM_CRUMBLE
#alias 2	:	FALLPLATFORM_FALL
#alias 3	:	FALLPLATFORM_RESTORE

// Property Values
#alias 0	:	FALLING_PLATFORM
#alias 1	:	STATIC_PLATFORM

// Priority
#alias 0	:	PRIORITY_BOUNDS
#alias 1	:	PRIORITY_ACTIVE

// Ink Effect Aliases
#alias 0 : INK_NONE   // Disables transparency
#alias 1 : INK_BLEND  // Uses half transparency
#alias 2 : INK_ALPHA  // Uses transparency value of Object.Alpha
#alias 3 : INK_ADD    // Uses transparency value of Object.Alpha, and a bright tint
#alias 4 : INK_SUB    // Uses transparency value of Object.Alpha, and a dark tint


sub ObjectMain
	if Object.Falling == true

		if Object.State == FALLPLATFORM_IDLE
			Object.State = FALLPLATFORM_CRUMBLE
		end if

		if Object.Timer < 16
			Object.Timer++

			TempValue0  = Object.Timer
			TempValue0 &= 3

			if TempValue0 == 3
				Object.YPlayerFloor = 0x10000
			else
				Object.YPlayerFloor = 0
			end if

		else
			Object.YPlayerFloor = 0
		end if

	else
		if Object.Timer > 0
			Object.Timer--

			TempValue0  = Object.Timer
			TempValue0 &= 3

			if TempValue0 == 3
				Object.YPlayerFloor = -0x10000
			else
				Object.YPlayerFloor = 0
			end if

		else
			Object.YPlayerFloor = 0
		end if

	end if
	Object.Falling = false

	if Object.PropertyValue == FALLING_PLATFORM
		switch Object.State
		case FALLPLATFORM_CRUMBLE
			if Object.FallTimer < 30
				Object.FallTimer++
			else
				Object.YVelocity = 0
				Object.FallTimer = 0
				Object.State = FALLPLATFORM_FALL
				Object.Priority = PRIORITY_ACTIVE
			end if
			break

		case FALLPLATFORM_FALL
			if Object.YVelocity < 0x40000
				Object.YVelocity += 0x2000
			end if
			Object.YPlayerFloor += Object.YVelocity
			if Object.OutOfBounds == true
				Object.YPos = Object.YOriginPos
				Object.State = FALLPLATFORM_RESTORE
				Object.YVelocity = 0
			end if
			break

		case FALLPLATFORM_RESTORE
			if Object.OutOfBounds == true
				Object.State = FALLPLATFORM_IDLE
				Object.Priority = PRIORITY_BOUNDS
			end if
			break
		end switch
	end if
end sub


sub ObjectPlayerInteraction
	if Object.State < FALLPLATFORM_RESTORE
		PlayerObjectCollision(C_PLATFORM, -32, -9, 32, 12)
		if CheckResult == true
			Object.Falling = true
			Player.YPos += Object.YPlayerFloor
		end if
	end if
end sub


sub ObjectDraw
	Object.YPos += Object.YPlayerFloor
	if Object.State < FALLPLATFORM_RESTORE
		DrawSprite(0)
	end if
end sub


sub ObjectStartup
	LoadSpriteSheet("R4.5/Objects.gif")

	SpriteFrame(-32, -9, 64, 24, 60, 1)	// #0 - Platform

	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Fall Platform]
			Object[ArrayPos0].YOriginPos = Object[ArrayPos0].YPos
		end if
		ArrayPos0++
	loop
end sub


// ========================
// Editor Subs
// ========================

sub RSDKEdit
	if Editor.ReturnVariable == true
		switch Editor.VariableID
		case EDIT_VAR_PROPVAL // Property Value
			CheckResult  = Object.PropertyValue
			CheckResult &= 1
			break
		case 0 // type
			CheckResult  = Object.PropertyValue
			CheckResult &= 1
			break
		end switch
	else
		switch Editor.VariableID
		case EDIT_VAR_PROPVAL // Property Value
			Object.PropertyValue  = Editor.VariableValue
			Object.PropertyValue &= 1
			break
		case 0 // type
			Object.PropertyValue  = Editor.VariableValue
			Object.PropertyValue &= 1
			break
		end switch
	end if
end sub


sub RSDKDraw
	DrawSprite(0)

	if editor.showGizmos == true
		editor.drawingOverlay = true

		TempValue0 = Object.YPos
		TempValue0 += 0x60 0000
		DrawArrow(Object.XPos, Object.YPos, Object.XPos, TempValue0, 0xFF, 0xFF, 0xFF)
			
		editor.drawingOverlay = false
	end if
end sub


sub RSDKLoad
	LoadSpriteSheet("R4.5/Objects.gif")
	SpriteFrame(-32, -9, 64, 24, 60, 1)	// #0 - Platform

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Falling Platform", 0)
	AddEnumVariable("Static Platform", 1)
end sub
